<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- :row行 col:列 -->
        <el-row
          type="flex"
          justify="space-between"
          align="middle"
          style="height: 40px"
        >
          <el-col :span="20">
            <svg-icon icon-class="user" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span> 操作<i class="el-icon-arrow-down" /> </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item>添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <!-- 树状组件 -->
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="tan(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="bianji(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="shanchu(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>
    </div>
    <el-dialog
      :title="isEdit ? '修改' : '添加'"
      :visible.sync="showDialog"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
    >
      <AddorEdit v-if="showDialog" :id="usid" :isedit="isEdit" :oragin="oraginlist" @genxin="shuju" @close="hClose" />
      <!-- v-if销毁弹出层以便来更新数据 -->

    </el-dialog>
  </div>
</template>
<!-- 放置一个el-tree组件 -->

<script>
import { getDepartments, delDepartment } from '@/api/departments'// 查询企业的部门列表
import { tranListToTreeData } from '@/utils/index'// 引入函数来处理chichildren
import AddorEdit from './deptDialog.vue'// 引入弹窗内容组件
export default {
  components: {
    AddorEdit
  },
  data() {
    return {
      usid: '', // 传入子组件id
      // 依赖一份树形数据
      showDialog: false, // 是否显示弹层
      list: [

      ],
      isEdit: false, // 是否处于编辑状态
      oraginlist: []// 取id,name,pid,code

    }
  },
  created() {
    this.loadDepartments()// 获取数据
  },
  methods: {
    async loadDepartments() {
      const res = await getDepartments()
      console.log('部门列表', res)
      // shift可以取出数组的第一个值，然后需要单独把他拿出来，让他去管理所有的，拿出来将他赋值给a

      const a = res.data.depts.shift()
      this.oraginlist = res.data.depts.map(({ id, pid, code, name }) => {
        return { id, pid, code, name }// 结构赋值将他们单独拿出来，给了oraginlist，传给子组件让他去验证
      })
      // a.children属性等于用函数处理过的剩下的数组，把它们包起来
      a.children = tranListToTreeData(res.data.depts)
      // 最后再让这个数组等于准备好的数组【a】
      this.list = [a]
    },
    tan(id) { // 添加按钮
      this.showDialog = true
      this.usid = id// 将id值给usid让他传给子组件
      this.isEdit = false// 更新编辑状态
    },
    bianji(id) { // 编辑按钮
      this.isEdit = true// 更新编辑状态
      this.showDialog = true// 弹出层显示
      this.usid = id// 将id值给usid让他传给子组件
    },
    shuju() {
      this.showDialog = false// 关闭弹窗
      this.loadDepartments()// 更新数据
    },
    // 删除数据
    async doDel(id) {
      try {
        const res = await delDepartment(id)
        console.log('删除', res)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.loadDepartments()// 获取数据
      } catch (err) {
        this.$message({
          type: 'error',
          message: '删除失败'
        })
        console.log(err)
      }
    },
    shanchu(id) { // 删除数据
      this.$confirm('此操作将永久删除此部门, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'

      }).then(() => {
        this.doDel(id)// 根据id删除数据
      }).catch(() => {

      })
    },
    hClose() {
      this.showDialog = false// 点取消关闭弹窗
    }
  }
}
</script>
